package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.mapper.AlbumStatMapper;
import com.atguigu.tingshu.album.mapper.TrackStatMapper;
import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.common.login.TsLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.AlbumStat;
import com.atguigu.tingshu.model.album.TrackStat;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumInfoVo;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "专辑管理")
@RestController
@RequestMapping("api/album/albumInfo")
@SuppressWarnings({"all"})
public class AlbumInfoApiController {

    @Autowired
    private AlbumInfoService albumInfoService;
    @TsLogin
    @Operation(summary = "保存专辑")
    @PostMapping("saveAlbumInfo")
    public Result saveAlbumInfo(@RequestBody @Validated AlbumInfoVo albumInfoVo) {
        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
        albumInfoService.saveAlbumInfo(albumInfoVo,userId);
        return Result.ok();
    }

    @TsLogin
    @Operation(summary = "查询专辑分页列表")
    @PostMapping("findUserAlbumPage/{pageNo}/{pageSize}")
    public Result findUserAlbumPage(@PathVariable Long pageNo,
                                    @PathVariable Long pageSize,
                                    @RequestBody AlbumInfoQuery albumInfoQuery){

        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
        albumInfoQuery.setUserId(userId);
        Page<AlbumListVo> albumInfoPage = new Page<>(pageNo,pageSize);
        IPage<AlbumListVo> albumInfoList =  albumInfoService.findUserAlbumPage(albumInfoPage,albumInfoQuery);
        return Result.ok(albumInfoList);
    }

    @Operation(summary = "删除专辑")
    @DeleteMapping("removeAlbumInfo/{id}")
    public Result removeAlbumInfoById(@PathVariable Long id){
        albumInfoService.removeAlbumInfoById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id获取专辑信息")
    @GetMapping("getAlbumInfo/{id}")
    public Result getAlbumInfoById(@PathVariable Long id){
        return Result.ok(albumInfoService.getAlbumInfoById(id));
    }
    @Operation(summary = "根据id修改专辑信息")
    @PutMapping("/updateAlbumInfo/{id}")
    public Result  updateById(@PathVariable Long id,@RequestBody @Validated AlbumInfoVo albumInfoVo) {
        //	调用服务层方法
        albumInfoService.updateAlbumInfo(id, albumInfoVo);
        return Result.ok();

    }
    @Operation(summary = "获取当前用户全部专辑列表")
    @GetMapping("findUserAllAlbumList")
    public Result findUserAllAlbumList(){
        Long userId = AuthContextHolder.getUserId() == null ? 1l : AuthContextHolder.getUserId();
        List<AlbumInfo> albumInfoList =  albumInfoService.findUserAllAlbumList(userId);
        return Result.ok(albumInfoList);
    }
    @Operation(summary = "根据专辑id获取专辑统计信息")
    @GetMapping("getAlbumStat/{id}")
    public Result getAlbumStat(@PathVariable Long id){
        AlbumStatVo albumStatVo = albumInfoService.getAlbumStat(id);
        return Result.ok(albumStatVo);
    }



}

